package Graph;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Graph {
    private ArrayList<String> vertexList; //存储顶点集合
    private int[][] edges; //存储图对应的邻结矩阵
    private int numOfEdges; // 表示边的数目

    private boolean[] isVistited;

    public static void main(String[] args) {

        String VertexValue[] = {"A", "B", "C", "D", "E"};
        int n = 5;
        Graph graph = new Graph(n);
        //循环添加
        for (String value : VertexValue) {
            graph.insertVertex(value);
        }
        //A-B A-C B-C B-D B-E
        graph.insertEdge(0, 1, 1);
        graph.insertEdge(0, 2, 1);
        graph.insertEdge(1, 2, 1);
        graph.insertEdge(1, 3, 1);
        graph.insertEdge(1, 4, 1);
        graph.showGraph(VertexValue);

        System.out.println(graph.getFist(4));

//        graph.dfs();
        System.out.println("广度");
        graph.bfs();
    }

    public Graph(int n) {
        //初始化矩阵
        edges = new int[n][n];
        vertexList = new ArrayList<String>(n);
        numOfEdges = 0;
        isVistited = new boolean[n];
    }

    //插入节点
    public void insertVertex(String vertex) {
        vertexList.add(vertex);
    }
    //添加边

    /**
     * @param v1     表示点的下标 A -B A-0 B-1
     * @param v2     第二个顶点对应的下标
     * @param weight
     */
    public void insertEdge(int v1, int v2, int weight) {
        edges[v1][v2] = weight;
        edges[v2][v1] = weight;
        numOfEdges++;

    }

    //该节点是否存在邻接节点  存在返回该节点的下标
    public int getFist(int index) {
        for (int j = 0; j < vertexList.size(); j++) {
            if (edges[index][j] > 0) {
                return j;
            }
        }
        return -1;
    }

    //根据前一个领接节点的下标来获取下一个邻接节点
    public int getNext(int v1, int v2) {
        for (int i = v2 + 1; i < vertexList.size(); i++) {
            if (edges[v1][i] > 0) {
                return i;
            }
        }
        return -1;
    }

    //深度优先算法
    public void dfs(boolean[] isVistited, int i) {
        //输出该节点
        System.out.println(getValueByIndex(i));
        //设置已访问
        isVistited[i] = true;
        //查找i邻接节点 是否存在
        int w = getFist(i);
        while (w != -1) { //有邻接节点
            if (!isVistited[w]) {
                dfs(isVistited, w);
            } else {
                w = getNext(i, w);
            }
        }
    }

    public void dfs() {
        for (int i = 0; i < getNumOfVertex(); i++) {
            if (!isVistited[i]) {
                dfs(isVistited, i);
            }
        }
    }
  public void bfs(){
      for (int i = 0; i < getNumOfVertex(); i++) {
          if (!isVistited[i]) {
              bfs(isVistited, i);
          }
      }
  }


    //对一个节点进行广度优先遍历的方法
    public void bfs(boolean[] isVistited, int i) {
        int u;//表示队列的头节点
        int w;//表示邻接节点
        //队列 纪录节点访问的顺序
        LinkedList queue = new LinkedList();
        //访问节点
        System.out.print(getValueByIndex(i) + "=>");
        isVistited[i] = true;
        //将节点加入队列
        queue.addLast(i);
        while (!queue.isEmpty()) {
            //取出队列的头结点
            u = (Integer) queue.removeFirst();
            w = getFist(u);
            while (w != -1) {
                if (!isVistited[w]) {
                    System.out.print(getValueByIndex(w) + "=>");
                    isVistited[w] = true;
                    queue.addLast(w);
                }
                w = getNext(u, w); //提现出广度优先
            }
        }
    }

    //返回节点个数
    public int getNumOfVertex() {
        return vertexList.size();
    }

    //得到边的数目
    public int getNumOfEdges() {
        return numOfEdges;
    }

    //返回节点i下标对应的数据
    public String getValueByIndex(int i) {
        return vertexList.get(i);
    }

    //返回v1和v2的权值
    public int getWeight(int v1, int v2) {
        return edges[v1][v2];
    }

    //显示图对应的矩阵
    public void showGraph(String[] strings) {
        Object[] array = vertexList.toArray();
        System.out.println(" " + Arrays.toString(array));
        for (int i = 0; i < edges.length; i++) {
            System.out.print(array[i]);
            System.out.println(Arrays.toString(edges[i]));
        }
    }

}
